﻿using System;
using System.Configuration;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KPIS.DBM;
using KPIS.GERP.GIMS.MODEL;
using KPIS.GERP.GIMS.IDAL;

namespace KPIS.GERP.GIMS.DAL
{
    public sealed class MasterCommitteeMappingDAL : IDALMasterCommitteeMapping
    {
        IDBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);

        public MasterCommitteeMappingDAL()
        {

        }

        public IList<MasterCommitteeMappingInfo> GetMasterCommitteeMapping()
        {
            string strSQL;
            DataSet ds = null;
            DataRowCollection dr = null;
            IList<MasterCommitteeMappingInfo> infoList = new List<MasterCommitteeMappingInfo>();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(1);

                dbManager.AddParameters(0, "@delete_status", SystemInfo._DeleteRecordStatus);

                strSQL = "SELECT cm.*"
                        + ", CASE WHEN cm.AMOUNT_TYPE = '<' THEN CONCAT('ไม่เกิน ', CAST(cm.MIN_AMOUNT AS CHAR), ' บาท')"
                            + " WHEN cm.AMOUNT_TYPE = '-' THEN CONCAT('ตั้งแต่ ', CAST(cm.MIN_AMOUNT AS CHAR), ' บาท ถึง ' , CAST(cm.MAX_AMOUNT AS CHAR), ' บาท')"
                            + " WHEN cm.AMOUNT_TYPE = '>' THEN CONCAT('ตั้งแต่ ', CAST(cm.MAX_AMOUNT AS CHAR), ' บาทขึ้นไป ')"
                            + " ELSE 'ไม่กำหนด'"
                        + " END AS AMOUNT_SHOW"
                        + ", cf.ICM_COM_FUNC_NAME"
                        + ", cp.ICM_COM_POS_NAME"
                        + " FROM ICM_MAS_COMMITTEE_MAPPING cm"
                            + " INNER JOIN ICM_MAS_PURCHASE_MAPPING pm ON cm.ICM_PC_MAP_SEQ = pm.ICM_PC_MAP_SEQ"
                            + " INNER JOIN ICM_MAS_COMMITTEE_FUNCTION cf ON cm.ICM_COM_FUNC_SEQ = cf.ICM_COM_FUNC_SEQ"
                            + " INNER JOIN ICM_MAS_COMMITTEE_POSITION cp ON cm.ICM_COM_POS_SEQ = cp.ICM_COM_POS_SEQ"
                        + " WHERE cm.RECORD_STATUS <> @delete_status";

                ds = dbManager.ExecuteDataSet(CommandType.Text, strSQL);
                dr = ds.Tables[0].Rows;
                if (dr.Count > 0)
                {
                    for (int i = 0; i < dr.Count; i++)
                    {
                        MasterCommitteeMappingInfo info = new MasterCommitteeMappingInfo();
                        MasterCommitteeFunctionInfo infoFunction = new MasterCommitteeFunctionInfo();
                        MasterCommitteePositionInfo infoPosition = new MasterCommitteePositionInfo();
                        RecordInfo infoRecord = new RecordInfo();
                        SysRecordStatusInfo infoStatus = new SysRecordStatusInfo();
                        info.Seq = Convert.ToInt32(dr[i]["ICM_COM_MAP_SEQ"]);
                        infoFunction.Seq = (Convert.IsDBNull(dr[i]["ICM_COM_FUNC_SEQ"]) ? null : (Nullable<int>)(dr[i]["ICM_COM_FUNC_SEQ"]));
                        infoFunction.Name = (dr[i]["ICM_COM_FUNC_NAME"]).ToString();
                        infoPosition.Seq = (Convert.IsDBNull(dr[i]["ICM_COM_POS_SEQ"]) ? null : (Nullable<int>)(dr[i]["ICM_COM_POS_SEQ"]));
                        infoPosition.Name = (dr[i]["ICM_COM_POS_NAME"]).ToString();
                        info.Amount_type = (dr[i]["AMOUNT_TYPE"]).ToString();
                        info.Amount_show = (dr[i]["AMOUNT_SHOW"]).ToString();
                        info.Min_amount = (Convert.IsDBNull(dr[i]["MIN_AMOUNT"]) ? null : (Nullable<double>)(dr[i]["MIN_AMOUNT"]));
                        info.Max_amount = (Convert.IsDBNull(dr[i]["MAX_AMOUNT"]) ? null : (Nullable<double>)(dr[i]["MAX_AMOUNT"]));
                        infoRecord.Created_by = (Convert.IsDBNull(dr[i]["CREATED_BY"]) ? null : (Nullable<int>)(dr[i]["CREATED_BY"]));
                        infoRecord.Created_when = (Convert.IsDBNull(dr[i]["CREATED_WHEN"]) ? null : (Nullable<DateTime>)(dr[i]["CREATED_WHEN"]));
                        infoRecord.Updated_by = (Convert.IsDBNull(dr[i]["UPDATED_BY"]) ? null : (Nullable<int>)(dr[i]["UPDATED_BY"]));
                        infoRecord.Updated_when = (Convert.IsDBNull(dr[i]["UPDATED_WHEN"]) ? null : (Nullable<DateTime>)(dr[i]["UPDATED_WHEN"]));
                        infoRecord.Restored_by = (Convert.IsDBNull(dr[i]["RESTORED_BY"]) ? null : (Nullable<int>)(dr[i]["RESTORED_BY"]));
                        infoRecord.Restored_when = (Convert.IsDBNull(dr[i]["RESTORED_WHEN"]) ? null : (Nullable<DateTime>)(dr[i]["RESTORED_WHEN"]));
                        infoStatus.Status = (dr[i]["RECORD_STATUS"]).ToString();
                        infoRecord.InfoStatus = infoStatus;
                        info.InfoFunction = infoFunction;
                        info.InfoPosition = infoPosition;
                        info.InfoRecord = infoRecord;
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
                //return null;
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }

        public IList<MasterCommitteeMappingInfo> GetMasterCommitteeMapping(string record_status)
        {
            string strSQL;
            DataSet ds = null;
            DataRowCollection dr = null;
            IList<MasterCommitteeMappingInfo> infoList = new List<MasterCommitteeMappingInfo>();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(1);

                dbManager.AddParameters(0, "@record_status", record_status);

                strSQL = "SELECT cm.*"
                        + ", CASE WHEN cm.AMOUNT_TYPE = '<' THEN CONCAT('ไม่เกิน ', CAST(cm.MIN_AMOUNT AS CHAR), ' บาท')"
                            + " WHEN cm.AMOUNT_TYPE = '-' THEN CONCAT('ตั้งแต่ ', CAST(cm.MIN_AMOUNT AS CHAR), ' บาท ถึง ' , CAST(cm.MAX_AMOUNT AS CHAR), ' บาท')"
                            + " WHEN cm.AMOUNT_TYPE = '>' THEN CONCAT('ตั้งแต่ ', CAST(cm.MAX_AMOUNT AS CHAR), ' บาทขึ้นไป ')"
                            + " ELSE 'ไม่กำหนด'"
                        + " END AS AMOUNT_SHOW"
                        + ", cf.ICM_COM_FUNC_NAME"
                        + ", cp.ICM_COM_POS_NAME"
                        + " FROM ICM_MAS_COMMITTEE_MAPPING cm"
                            + " INNER JOIN ICM_MAS_PURCHASE_MAPPING pm ON cm.ICM_PC_MAP_SEQ = pm.ICM_PC_MAP_SEQ"
                            + " INNER JOIN ICM_MAS_COMMITTEE_FUNCTION cf ON cm.ICM_COM_FUNC_SEQ = cf.ICM_COM_FUNC_SEQ"
                            + " INNER JOIN ICM_MAS_COMMITTEE_POSITION cp ON cm.ICM_COM_POS_SEQ = cp.ICM_COM_POS_SEQ"
                        + " WHERE cm.RECORD_STATUS = @record_status";

                ds = dbManager.ExecuteDataSet(CommandType.Text, strSQL);
                dr = ds.Tables[0].Rows;
                if (dr.Count > 0)
                {
                    for (int i = 0; i < dr.Count; i++)
                    {
                        MasterCommitteeMappingInfo info = new MasterCommitteeMappingInfo();
                        MasterCommitteeFunctionInfo infoFunction = new MasterCommitteeFunctionInfo();
                        MasterCommitteePositionInfo infoPosition = new MasterCommitteePositionInfo();
                        RecordInfo infoRecord = new RecordInfo();
                        SysRecordStatusInfo infoStatus = new SysRecordStatusInfo();
                        info.Seq = Convert.ToInt32(dr[i]["ICM_COM_MAP_SEQ"]);
                        infoFunction.Seq = (Convert.IsDBNull(dr[i]["ICM_COM_FUNC_SEQ"]) ? null : (Nullable<int>)(dr[i]["ICM_COM_FUNC_SEQ"]));
                        infoFunction.Name = (dr[i]["ICM_COM_FUNC_NAME"]).ToString();
                        infoPosition.Seq = (Convert.IsDBNull(dr[i]["ICM_COM_POS_SEQ"]) ? null : (Nullable<int>)(dr[i]["ICM_COM_POS_SEQ"]));
                        infoPosition.Name = (dr[i]["ICM_COM_POS_NAME"]).ToString();
                        info.Amount_type = (dr[i]["AMOUNT_TYPE"]).ToString();
                        info.Amount_show = (dr[i]["AMOUNT_SHOW"]).ToString();
                        info.Min_amount = (Convert.IsDBNull(dr[i]["MIN_AMOUNT"]) ? null : (Nullable<double>)(dr[i]["MIN_AMOUNT"]));
                        info.Max_amount = (Convert.IsDBNull(dr[i]["MAX_AMOUNT"]) ? null : (Nullable<double>)(dr[i]["MAX_AMOUNT"]));
                        infoRecord.Created_by = (Convert.IsDBNull(dr[i]["CREATED_BY"]) ? null : (Nullable<int>)(dr[i]["CREATED_BY"]));
                        infoRecord.Created_when = (Convert.IsDBNull(dr[i]["CREATED_WHEN"]) ? null : (Nullable<DateTime>)(dr[i]["CREATED_WHEN"]));
                        infoRecord.Updated_by = (Convert.IsDBNull(dr[i]["UPDATED_BY"]) ? null : (Nullable<int>)(dr[i]["UPDATED_BY"]));
                        infoRecord.Updated_when = (Convert.IsDBNull(dr[i]["UPDATED_WHEN"]) ? null : (Nullable<DateTime>)(dr[i]["UPDATED_WHEN"]));
                        infoRecord.Restored_by = (Convert.IsDBNull(dr[i]["RESTORED_BY"]) ? null : (Nullable<int>)(dr[i]["RESTORED_BY"]));
                        infoRecord.Restored_when = (Convert.IsDBNull(dr[i]["RESTORED_WHEN"]) ? null : (Nullable<DateTime>)(dr[i]["RESTORED_WHEN"]));
                        infoStatus.Status = (dr[i]["RECORD_STATUS"]).ToString();
                        infoRecord.InfoStatus = infoStatus;
                        info.InfoFunction = infoFunction;
                        info.InfoPosition = infoPosition;
                        info.InfoRecord = infoRecord;
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
                //return null;
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }

        public MasterCommitteeMappingInfo GetMasterCommitteeMapping(int seq)
        {
            string strSQL;
            DataSet ds;
            DataRowCollection dr;
            MasterCommitteeMappingInfo info = new MasterCommitteeMappingInfo();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(1);

                dbManager.AddParameters(0, "@seq", seq);

                strSQL = "SELECT cm.*"
                        + ", CASE WHEN cm.AMOUNT_TYPE = '<' THEN CONCAT('ไม่เกิน ', CAST(cm.MIN_AMOUNT AS CHAR), ' บาท')"
                            + " WHEN cm.AMOUNT_TYPE = '-' THEN CONCAT('ตั้งแต่ ', CAST(cm.MIN_AMOUNT AS CHAR), ' บาท ถึง ' , CAST(cm.MAX_AMOUNT AS CHAR), ' บาท')"
                            + " WHEN cm.AMOUNT_TYPE = '>' THEN CONCAT('ตั้งแต่ ', CAST(cm.MAX_AMOUNT AS CHAR), ' บาทขึ้นไป ')"
                            + " ELSE 'ไม่กำหนด'"
                        + " END AS AMOUNT_SHOW"
                        + ", cf.ICM_COM_FUNC_NAME"
                        + ", cp.ICM_COM_POS_NAME"
                        + " FROM ICM_MAS_COMMITTEE_MAPPING cm"
                            + " INNER JOIN ICM_MAS_PURCHASE_MAPPING pm ON cm.ICM_PC_MAP_SEQ = pm.ICM_PC_MAP_SEQ"
                            + " INNER JOIN ICM_MAS_COMMITTEE_FUNCTION cf ON cm.ICM_COM_FUNC_SEQ = cf.ICM_COM_FUNC_SEQ"
                            + " INNER JOIN ICM_MAS_COMMITTEE_POSITION cp ON cm.ICM_COM_POS_SEQ = cp.ICM_COM_POS_SEQ"
                        + " WHERE cm.ICM_COM_MAP_SEQ = @seq";

                ds = dbManager.ExecuteDataSet(CommandType.Text, strSQL);
                dr = ds.Tables[0].Rows;
                if (dr.Count > 0)
                {
                    MasterCommitteeFunctionInfo infoFunction = new MasterCommitteeFunctionInfo();
                    MasterCommitteePositionInfo infoPosition = new MasterCommitteePositionInfo();
                    RecordInfo infoRecord = new RecordInfo();
                    SysRecordStatusInfo infoStatus = new SysRecordStatusInfo();
                    info.Seq = Convert.ToInt32(dr[0]["ICM_COM_MAP_SEQ"]);
                    infoFunction.Seq = (Convert.IsDBNull(dr[0]["ICM_COM_FUNC_SEQ"]) ? null : (Nullable<int>)(dr[0]["ICM_COM_FUNC_SEQ"]));
                    infoFunction.Name = (dr[0]["ICM_COM_FUNC_NAME"]).ToString();
                    infoPosition.Seq = (Convert.IsDBNull(dr[0]["ICM_COM_POS_SEQ"]) ? null : (Nullable<int>)(dr[0]["ICM_COM_POS_SEQ"]));
                    infoPosition.Name = (dr[0]["ICM_COM_POS_NAME"]).ToString();
                    info.Amount_type = (dr[0]["AMOUNT_TYPE"]).ToString();
                    info.Amount_show = (dr[0]["AMOUNT_SHOW"]).ToString();
                    info.Min_amount = (Convert.IsDBNull(dr[0]["MIN_AMOUNT"]) ? null : (Nullable<double>)(dr[0]["MIN_AMOUNT"]));
                    info.Max_amount = (Convert.IsDBNull(dr[0]["MAX_AMOUNT"]) ? null : (Nullable<double>)(dr[0]["MAX_AMOUNT"]));
                    infoRecord.Created_by = (Convert.IsDBNull(dr[0]["CREATED_BY"]) ? null : (Nullable<int>)(dr[0]["CREATED_BY"]));
                    infoRecord.Created_when = (Convert.IsDBNull(dr[0]["CREATED_WHEN"]) ? null : (Nullable<DateTime>)(dr[0]["CREATED_WHEN"]));
                    infoRecord.Updated_by = (Convert.IsDBNull(dr[0]["UPDATED_BY"]) ? null : (Nullable<int>)(dr[0]["UPDATED_BY"]));
                    infoRecord.Updated_when = (Convert.IsDBNull(dr[0]["UPDATED_WHEN"]) ? null : (Nullable<DateTime>)(dr[0]["UPDATED_WHEN"]));
                    infoRecord.Restored_by = (Convert.IsDBNull(dr[0]["RESTORED_BY"]) ? null : (Nullable<int>)(dr[0]["RESTORED_BY"]));
                    infoRecord.Restored_when = (Convert.IsDBNull(dr[0]["RESTORED_WHEN"]) ? null : (Nullable<DateTime>)(dr[0]["RESTORED_WHEN"]));
                    infoStatus.Status = (dr[0]["RECORD_STATUS"]).ToString();
                    infoRecord.InfoStatus = infoStatus;
                    info.InfoFunction = infoFunction;
                    info.InfoPosition = infoPosition;
                    info.InfoRecord = infoRecord;
                }
            }
            catch (Exception ex)
            {
                throw (ex);
                //return null;
            }
            finally
            {
                dbManager.Dispose();
            }

            return info;
        }

        public IList<MasterCommitteeMappingInfo> GetMasterCommitteeMappingWithMethod(int map_seq)
        {
            string strSQL;
            DataSet ds = null;
            DataRowCollection dr = null;
            IList<MasterCommitteeMappingInfo> infoList = new List<MasterCommitteeMappingInfo>();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(2);

                dbManager.AddParameters(0, "@delete_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@map_seq", map_seq);

                strSQL = "SELECT cm.*"
                        + ", CASE WHEN cm.AMOUNT_TYPE = '<' THEN CONCAT('ไม่เกิน ', CAST(cm.MIN_AMOUNT AS CHAR), ' บาท')"
                            + " WHEN cm.AMOUNT_TYPE = '-' THEN CONCAT('ตั้งแต่ ', CAST(cm.MIN_AMOUNT AS CHAR), ' บาท ถึง ' , CAST(cm.MAX_AMOUNT AS CHAR), ' บาท')"
                            + " WHEN cm.AMOUNT_TYPE = '>' THEN CONCAT('ตั้งแต่ ', CAST(cm.MAX_AMOUNT AS CHAR), ' บาทขึ้นไป ')"
                            + " ELSE 'ไม่กำหนด'"
                        + " END AS AMOUNT_SHOW"
                        + ", cf.ICM_COM_FUNC_NAME"
                        + ", cp.ICM_COM_POS_NAME"
                        + " FROM ICM_MAS_COMMITTEE_MAPPING cm"
                            + " INNER JOIN ICM_MAS_PURCHASE_MAPPING pm ON cm.ICM_PC_MAP_SEQ = pm.ICM_PC_MAP_SEQ"
                            + " INNER JOIN ICM_MAS_COMMITTEE_FUNCTION cf ON cm.ICM_COM_FUNC_SEQ = cf.ICM_COM_FUNC_SEQ"
                            + " INNER JOIN ICM_MAS_COMMITTEE_POSITION cp ON cm.ICM_COM_POS_SEQ = cp.ICM_COM_POS_SEQ"
                        + " WHERE cm.RECORD_STATUS <> @delete_status"
                            + " AND cm.ICM_PC_MAP_SEQ = @map_seq";

                ds = dbManager.ExecuteDataSet(CommandType.Text, strSQL);
                dr = ds.Tables[0].Rows;
                if (dr.Count > 0)
                {
                    for (int i = 0; i < dr.Count; i++)
                    {
                        MasterCommitteeMappingInfo info = new MasterCommitteeMappingInfo();
                        MasterCommitteeFunctionInfo infoFunction = new MasterCommitteeFunctionInfo();
                        MasterCommitteePositionInfo infoPosition = new MasterCommitteePositionInfo();
                        RecordInfo infoRecord = new RecordInfo();
                        SysRecordStatusInfo infoStatus = new SysRecordStatusInfo();
                        info.Seq = Convert.ToInt32(dr[i]["ICM_COM_MAP_SEQ"]);
                        infoFunction.Seq = (Convert.IsDBNull(dr[i]["ICM_COM_FUNC_SEQ"]) ? null : (Nullable<int>)(dr[i]["ICM_COM_FUNC_SEQ"]));
                        infoFunction.Name = (dr[i]["ICM_COM_FUNC_NAME"]).ToString();
                        infoPosition.Seq = (Convert.IsDBNull(dr[i]["ICM_COM_POS_SEQ"]) ? null : (Nullable<int>)(dr[i]["ICM_COM_POS_SEQ"]));
                        infoPosition.Name = (dr[i]["ICM_COM_POS_NAME"]).ToString();
                        info.Amount_type = (dr[i]["AMOUNT_TYPE"]).ToString();
                        info.Amount_show = (dr[i]["AMOUNT_SHOW"]).ToString();
                        info.Min_amount = (Convert.IsDBNull(dr[i]["MIN_AMOUNT"]) ? null : (Nullable<double>)(dr[i]["MIN_AMOUNT"]));
                        info.Max_amount = (Convert.IsDBNull(dr[i]["MAX_AMOUNT"]) ? null : (Nullable<double>)(dr[i]["MAX_AMOUNT"]));
                        infoRecord.Created_by = (Convert.IsDBNull(dr[i]["CREATED_BY"]) ? null : (Nullable<int>)(dr[i]["CREATED_BY"]));
                        infoRecord.Created_when = (Convert.IsDBNull(dr[i]["CREATED_WHEN"]) ? null : (Nullable<DateTime>)(dr[i]["CREATED_WHEN"]));
                        infoRecord.Updated_by = (Convert.IsDBNull(dr[i]["UPDATED_BY"]) ? null : (Nullable<int>)(dr[i]["UPDATED_BY"]));
                        infoRecord.Updated_when = (Convert.IsDBNull(dr[i]["UPDATED_WHEN"]) ? null : (Nullable<DateTime>)(dr[i]["UPDATED_WHEN"]));
                        infoRecord.Restored_by = (Convert.IsDBNull(dr[i]["RESTORED_BY"]) ? null : (Nullable<int>)(dr[i]["RESTORED_BY"]));
                        infoRecord.Restored_when = (Convert.IsDBNull(dr[i]["RESTORED_WHEN"]) ? null : (Nullable<DateTime>)(dr[i]["RESTORED_WHEN"]));
                        infoStatus.Status = (dr[i]["RECORD_STATUS"]).ToString();
                        infoRecord.InfoStatus = infoStatus;
                        info.InfoFunction = infoFunction;
                        info.InfoPosition = infoPosition;
                        info.InfoRecord = infoRecord;
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
                //return null;
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }

        public IList<MasterCommitteeMappingInfo> GetMasterCommitteeMappingWithFunction(int map_seq, int func_seq)
        {
            string strSQL;
            DataSet ds = null;
            DataRowCollection dr = null;
            IList<MasterCommitteeMappingInfo> infoList = new List<MasterCommitteeMappingInfo>();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(3);

                dbManager.AddParameters(0, "@delete_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@map_seq", map_seq);
                dbManager.AddParameters(2, "@func_seq", func_seq);

                strSQL = "SELECT cm.*"
                        + ", cf.ICM_COM_FUNC_NAME"
                        + ", cp.ICM_COM_POS_NAME"
                        + " FROM ICM_MAS_COMMITTEE_MAPPING cm"
                            + " INNER JOIN ICM_MAS_PURCHASE_MAPPING pm ON cm.ICM_PC_MAP_SEQ = pm.ICM_PC_MAP_SEQ"
                            + " INNER JOIN ICM_MAS_COMMITTEE_FUNCTION cf ON cm.ICM_COM_FUNC_SEQ = cf.ICM_COM_FUNC_SEQ"
                            + " INNER JOIN ICM_MAS_COMMITTEE_POSITION cp ON cm.ICM_COM_POS_SEQ = cp.ICM_COM_POS_SEQ"
                        + " WHERE cm.RECORD_STATUS <> @delete_status"
                            + " AND cm.ICM_PC_MAP_SEQ = @map_seq"
                            + " AND cm.ICM_COM_FUNC_SEQ = @func_seq";

                ds = dbManager.ExecuteDataSet(CommandType.Text, strSQL);
                dr = ds.Tables[0].Rows;
                if (dr.Count > 0)
                {
                    for (int i = 0; i < dr.Count; i++)
                    {
                        MasterCommitteeMappingInfo info = new MasterCommitteeMappingInfo();
                        MasterCommitteeFunctionInfo infoFunction = new MasterCommitteeFunctionInfo();
                        MasterCommitteePositionInfo infoPosition = new MasterCommitteePositionInfo();
                        info.Seq = Convert.ToInt32(dr[i]["ICM_COM_MAP_SEQ"]);
                        infoFunction.Seq = (Convert.IsDBNull(dr[i]["ICM_COM_FUNC_SEQ"]) ? null : (Nullable<int>)(dr[i]["ICM_COM_FUNC_SEQ"]));
                        infoFunction.Name = (dr[i]["ICM_COM_FUNC_NAME"]).ToString();
                        infoPosition.Seq = (Convert.IsDBNull(dr[i]["ICM_COM_POS_SEQ"]) ? null : (Nullable<int>)(dr[i]["ICM_COM_POS_SEQ"]));
                        infoPosition.Name = (dr[i]["ICM_COM_POS_NAME"]).ToString();
                        info.Amount_type = (dr[i]["AMOUNT_TYPE"]).ToString();
                        info.InfoFunction = infoFunction;
                        info.InfoPosition = infoPosition;
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
                //return null;
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }

        public int GetMaxNumberComFunction( int icm_pc_map_seq)
        {
            int number_com_func =0;
            string strSQL="";
            DataRowCollection drc = null;
            MasterCommitteeMappingInfo info = new MasterCommitteeMappingInfo();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(1);
                dbManager.AddParameters(0, "@icm_pc_map_seq", icm_pc_map_seq);

                strSQL = " SELECT  pc_type.icm_pc_type_name,pc_method.icm_pc_method_name,pc_method.icm_pc_method_seq ,cm.icm_pc_map_seq  ,cm.icm_com_func_seq, cf.ICM_COM_FUNC_NAME  "
                        +" , CASE WHEN cm.AMOUNT_TYPE = '<' THEN CONCAT('ไม่เกิน ', CAST(cm.MIN_AMOUNT AS CHAR), ' บาท') "
                        +" WHEN cm.AMOUNT_TYPE = '-' THEN CONCAT('ตั้งแต่ ', CAST(cm.MIN_AMOUNT AS CHAR), ' บาท ถึง ' , CAST(cm.MAX_AMOUNT AS CHAR), ' บาท') "
                        +" WHEN cm.AMOUNT_TYPE = '>' THEN CONCAT('ตั้งแต่ ', CAST(cm.MAX_AMOUNT AS CHAR), ' บาทขึ้นไป ') "
                        +" ELSE 'ไม่กำหนด' "
                        +" END AS AMOUNT_SHOW "
                        +" ,case when (max(number_com_func) is null) then 1 else max(number_com_func) +1 end number_com_func "
                        +"  FROM ICM_MAS_COMMITTEE_MAPPING cm "
                        +" INNER JOIN ICM_MAS_PURCHASE_MAPPING pm ON cm.ICM_PC_MAP_SEQ = pm.ICM_PC_MAP_SEQ "
                        +" INNER JOIN ICM_MAS_COMMITTEE_FUNCTION cf ON cm.ICM_COM_FUNC_SEQ = cf.ICM_COM_FUNC_SEQ "
                        +" INNER JOIN ICM_MAS_COMMITTEE_POSITION cp ON cm.ICM_COM_POS_SEQ = cp.ICM_COM_POS_SEQ "
                        +" LEFT JOIN ICM_MAS_PURCHASE_METHOD pc_method ON pm.icm_pc_method_seq = pc_method.icm_pc_method_seq "
                        +" LEFT JOIN icm_mas_purchase_type pc_type ON pc_method.icm_pc_type_seq = pc_type.icm_pc_type_seq "
                        +" GROUP BY icm_pc_method_seq ,icm_pc_type_name   ,cm.icm_com_func_seq "
                        + " HAVING  icm_pc_map_seq = @icm_pc_map_seq "
                        +" ORDER BY icm_pc_method_name,AMOUNT_SHOW  ,number_com_func ";

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;

                if (drc.Count > 0)
                {
                    number_com_func = Convert.ToInt32(drc[0]["number_com_func"]);
                }
                else if (drc.Count == 0)
                {
                    number_com_func = 1;
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return number_com_func;

        }

        public void Insert(IList<MasterCommitteeMappingInfo> infoList)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                for (int i = 0; i < infoList.Count; i++)
                {
                    dbManager.CreateParameters(10);

                    dbManager.AddParameters(0, "@map_seq", infoList[i].InfoPurchaseMapping.Seq);
                    dbManager.AddParameters(1, "@func_seq", infoList[i].InfoFunction.Seq);
                    dbManager.AddParameters(2, "@pos_seq", infoList[i].InfoPosition.Seq);
                    dbManager.AddParameters(3, "@amount_type", infoList[i].Amount_type);
                    dbManager.AddParameters(4, "@min_amount", infoList[i].Min_amount);
                    dbManager.AddParameters(5, "@max_amount", infoList[i].Max_amount);
                    dbManager.AddParameters(6, "@number_com_func" ,infoList[i].NUMBER_COM_FUNC);
                    dbManager.AddParameters(7, "@record_status", SystemInfo._WorkRecordStatus);
                    dbManager.AddParameters(8, "@created_by", infoList[i].InfoRecord.Created_by);
                    dbManager.AddParameters(9, "@created_when", infoList[i].InfoRecord.Created_when);

                    strSQL = "INSERT INTO ICM_MAS_COMMITTEE_MAPPING"
                        + " (ICM_PC_MAP_SEQ, ICM_COM_FUNC_SEQ, ICM_COM_POS_SEQ, AMOUNT_TYPE, MIN_AMOUNT, MAX_AMOUNT ,NUMBER_COM_FUNC "
                        + " , RECORD_STATUS, CREATED_BY, CREATED_WHEN)"
                        + " VALUES(@map_seq"
                        + ", @func_seq"
                        + ", @pos_seq"
                        + ", @amount_type"
                        + ", @min_amount"
                        + ", @max_amount"
                        + ", @number_com_func"
                        + ", @record_status"
                        + ", @created_by"
                        + ", @created_when"
                        + " )";

                    dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                }
                dbManager.CommitTransaction();
            }
            catch (Exception ex)
            {
                throw (ex);
                dbManager.RollBackTransaction();
                //throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void Update(MasterCommitteeMappingInfo info)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(10);

                dbManager.AddParameters(0, "@seq", info.Seq);
                dbManager.AddParameters(1, "@map_seq", info.InfoPurchaseMapping.Seq);
                dbManager.AddParameters(2, "@func_seq", info.InfoFunction.Seq);
                dbManager.AddParameters(3, "@pos_seq", info.InfoPosition.Seq);
                dbManager.AddParameters(4, "@amount_type", info.Amount_type);
                dbManager.AddParameters(5, "@min_amount", info.Min_amount);
                dbManager.AddParameters(6, "@max_amount", info.Max_amount);
                dbManager.AddParameters(7, "@record_status", SystemInfo._WorkRecordStatus);
                dbManager.AddParameters(8, "@updated_by", info.InfoRecord.Updated_by);
                dbManager.AddParameters(9, "@updated_when", info.InfoRecord.Updated_when);

                strSQL = "UPDATE ICM_MAS_COMMITTEE_MAPPING"
                    + " SET ICM_PC_MAP_SEQ = @map_seq"
                    + ", ICM_COM_FUNC_SEQ = @func_seq"
                    + ", ICM_COM_POS_SEQ = @pos_seq"
                    + ", AMOUNT_TYPE = @amount_type"
                    + ", MIN_AMOUNT = @min_amount"
                    + ", MAX_AMOUNT = @max_amount"
                    + ", RECORD_STATUS = @record_status"
                    + ", UPDATED_BY = @updated_by"
                    + ", UPDATED_WHEN = @updated_when"
                    + " WHERE ICM_COM_MAP_SEQ = @seq";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch
            {
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void Delete(MasterCommitteeMappingInfo info)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(4);

                dbManager.AddParameters(0, "@seq", info.Seq);
                dbManager.AddParameters(1, "@record_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(2, "@updated_by", info.InfoRecord.Updated_by);
                dbManager.AddParameters(3, "@updated_when", info.InfoRecord.Updated_when);

                strSQL = "UPDATE ICM_MAS_COMMITTEE_MAPPING"
                    + " SET RECORD_STATUS = @record_status"
                    + ", UPDATED_BY = @updated_by"
                    + ", UPDATED_WHEN = @updated_when"
                    + " WHERE ICM_COM_MAP_SEQ = @seq";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch
            {
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void DeleteAll(IList<MasterCommitteeMappingInfo> infoList)
        {
            string strSQL;
            string seq = "";
            for (int i = 0; i < infoList.Count; i++)
            {
                if (i == 0)
                {
                    seq += infoList[i].Seq;
                }
                else
                {
                    seq += "," + infoList[i].Seq;
                }
            }

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(3);

                dbManager.AddParameters(0, "@record_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@updated_by", infoList[0].InfoRecord.Updated_by);
                dbManager.AddParameters(2, "@updated_when", infoList[0].InfoRecord.Updated_when);

                strSQL = "UPDATE ICM_MAS_COMMITTEE_MAPPING"
                    + " SET RECORD_STATUS = @record_status"
                    + ", UPDATED_BY = @updated_by"
                    + ", UPDATED_WHEN = @updated_when"
                    + " WHERE ICM_COM_MAP_SEQ IN (" + seq + ")";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void DeleteFromDB(IList<MasterCommitteeMappingInfo> infoList)
        {
            string strSQL;
            string seq = "";
            for (int i = 0; i < infoList.Count; i++)
            {
                if (i == 0)
                {
                    seq += infoList[i].Seq;
                }
                else
                {
                    seq += "," + infoList[i].Seq;
                }
            }

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();

                strSQL = "DELETE FROM ICM_MAS_COMMITTEE_MAPPING"
                    + " WHERE ICM_COM_MAP_SEQ IN (" + seq + ")";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void Restore(IList<MasterCommitteeMappingInfo> infoList)
        {
            string strSQL;
            string seq = "";
            for (int i = 0; i < infoList.Count; i++)
            {
                if (i == 0)
                {
                    seq += infoList[i].Seq;
                }
                else
                {
                    seq += "," + infoList[i].Seq;
                }
            }

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(3);

                dbManager.AddParameters(0, "@record_status", SystemInfo._WorkRecordStatus);
                dbManager.AddParameters(1, "@restored_by", infoList[0].InfoRecord.Restored_by);
                dbManager.AddParameters(2, "@restored_when", infoList[0].InfoRecord.Restored_when);

                strSQL = "UPDATE ICM_MAS_COMMITTEE_MAPPING"
                    + " SET RECORD_STATUS = @record_status"
                    + ", RESTORED_BY = @restored_by"
                    + ", RESTORED_WHEN = @restored_when"
                    + " WHERE ICM_COM_MAP_SEQ IN (" + seq + ")";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
    }
}
